package com.odais.AE_Planner;

import java.util.ArrayList;

import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.adt.align.HorizontalAlign;

import com.odais.Reservation.ReservationBox;
import com.odais.Reservation.ReservationEntity;

import android.graphics.Color;
import android.graphics.Typeface;


/**
 *
 * @author Lionel DECELLE
 * @since 8 juin 2012 - 10:50:58
 *
 */
public class Table extends RoomObject
{
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Fields
// ===========================================================

	private SceneContext sceneContext;
	private int mSeatTotal;
	private boolean mIsEmpty;
	private BitmapTextureAtlas mFontTexture;
    private Font mFont;
    private Text textCenter;
    private ReservationEntity reservationEntity;
    private ArrayList<ReservationEntity> reservationEntityList;
    private int itemNumber;
    TableExpanded tableExpanded;


// ===========================================================
// Constructors
// ===========================================================
	public Table(SceneContext sceneContext, float mPosX, float mPosY, int mSeatTotal,
			boolean mIsEmpty, int itemNumber) {
		super(sceneContext, "table.png", mPosX, mPosY);
		this.sceneContext = sceneContext;
		this.mSeatTotal = mSeatTotal;
		this.mIsEmpty = mIsEmpty;
		this.reservationEntityList = new ArrayList<ReservationEntity>();

		this.mFont = FontFactory.create(sceneContext.getEngine().getFontManager(), sceneContext.getEngine().getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.NORMAL), 20, Color.WHITE);
		this.mFont.load();

		VertexBufferObjectManager vertexBufferObjectManager = sceneContext.getEngine().getVertexBufferObjectManager();
		textCenter = new Text(mPosX+this.getWidth()/2, mPosY, this.mFont, Integer.toString(this.reservationEntityList.size()), 4, vertexBufferObjectManager);

		sceneContext.getScene().attachChild(textCenter);

		tableExpanded = new TableExpanded(this, this.sceneContext, this.reservationEntityList);
		this.itemNumber = itemNumber;
	}


// ===========================================================
// Getter & Setter
// ===========================================================
	public int getItemNumber() {
		return itemNumber;
	}


	public void setItemNumber(int itemNumber) {
		this.itemNumber = itemNumber;
	}

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

	public ReservationEntity getReservation() {
		return reservationEntity;
	}


	public boolean setReservation(ReservationEntity reservationEntity) {
		this.reservationEntity = reservationEntity;

		if(this.reservationEntityList.size()<mSeatTotal)
		{
			this.reservationEntityList.add(this.reservationEntity);
			this.textCenter.setText(Integer.toString(this.reservationEntityList.size()));

			this.reservationEntity.getReservationBox_RightPanel().setVisible(false);
			this.reservationEntity.getReservationBox_RightPanel().setBackgroundColor(0.5f, 0.6f, 0.6f);

			if(this.reservationEntityList.size()==mSeatTotal)
			{
				super.getSprite().setColor(1, 0, 0);
				this.mIsEmpty = false;
			}
			else
			{
				super.getSprite().setColor(0, 1, 0);
				this.mIsEmpty = true;
			}
			return true;
		}
		else
		{
			return false;
		}
	}

	public void expandReservationList()
	{
		//Create new TableExpanded
		this.tableExpanded.show();
	}

	public void contractReservationList()
	{
		//this.tableExpanded.kill();
	}

}
